package com.sg.service.impl;

import com.github.pagehelper.PageHelper;
import com.sg.bean.User;
import com.sg.dao.UserDao;
import com.sg.service.UserService;
import com.sg.util.MD5Util;
import com.sg.vo.ChartInfo;
import com.sg.vo.UserVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangcf
 * @Description
 * @date 2023/3/14 10:25
 */
@Service
public class UserServiceImpl implements UserService {

    Logger logger = Logger.getLogger(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;

    @Override
    public List<UserVO> findAllPage(int pageNum, int pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        return userDao.findAll();
    }

    @Override
    public List<UserVO> findAll() {
        return userDao.findAll();
    }

    @Override
    public User findByUId(int uId) {
        return userDao.findByUId(uId);
    }

    @Override
    public UserVO login(String tel, String pass) {

        pass = MD5Util.md5(pass);//对于用户传入的密码进行加密算法
        return userDao.findByTelAndPass(tel,pass);
    }

    @Override
    public List<UserVO> find(User user) {
        return userDao.find(user);
    }

    @Override
    public boolean register(User user) {
        user.setuPass(MD5Util.md5(user.getuPass()));
        return userDao.add(user)>0?true:false;
    }

    @Override
    public boolean delete(User user) {
        return userDao.delete(user)>0?true:false;
    }

    @Override
    public boolean update(User user) {
        return userDao.update(user)>0?true:false;
    }

    @Override
    public List<ChartInfo> findStatPie() {
        return userDao.findStat();
    }

    @Override
    public Map<String, List> findStatBar() {
        Map<String, List> map = new HashMap<>();
        List<ChartInfo> list = userDao.findStat();
        List<String> names = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        for (ChartInfo chartInfo:list) {
            names.add(chartInfo.getName());
            values.add(chartInfo.getValue());
        }
        map.put("names",names);
        map.put("values",values);
        return map;
    }
}
